const path = require('path')
const { DefinePlugin } = require('webpack')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')

const CssLoader = (importLoaders) => {
  return {
    loader: 'css-loader',
    options: {
      importLoaders: importLoaders,
      esModule: false
    }
  }
}

module.exports = {
  mode: 'development',
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
    // 设置asset资源【全局】
    // assetModuleFilename: 'img/[name].[hash:4][ext]'
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          'style-loader', 
          CssLoader(1), 
          'postcss-loader'
        ]
      }, 
      {
        test: /\.less$/,
        use: [
          'style-loader', 
          CssLoader(1),
          'postcss-loader', 
          'less-loader'
        ]
      }, 
      {
        test: /\.(png|svg|gif|jpe?g)$/,
        type: 'asset',
        generator: {
          filename: 'img/[name].[hash:4][ext]'
        },
        parser: {
          dataUrlCondition: {
            maxSize: 4 * 1024
          }
        }
      },
      {
        test: /\.(ttf|woff2?|eot|svg|otf)$/,
        type: 'asset/resource',
        generator: {
          filename: 'fonts/[name].[hash:4][ext]'
        }
      }
    ]
  },
  plugins: [
    new CleanWebpackPlugin(),
    new HtmlWebpackPlugin({
      title: 'webpack666',
      template: './public/index.html'
    }),
    new DefinePlugin({
      BASE_URL: '"./"'
    })
  ]
}